package com.keensense.vcm.internal;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.keensense.vcm.HttpMethod;
import com.keensense.vcm.common.auth.CredentialsProvider;
import com.keensense.vcm.common.comm.RequestMessage;
import com.keensense.vcm.common.comm.ServiceClient;
import com.keensense.vcm.common.parser.ResponseParsers;
import com.keensense.vcm.model.*;

import static com.keensense.vcm.common.parser.ResponseParsers.*;

/**
 * 智能检索
 *
 * @author lanjian
 * @date 2020/4/29 12:56 下午
 */
public class IntelligentSearchOperation extends VCMOperation {

    public IntelligentSearchOperation(ServiceClient client, CredentialsProvider credsProvider) {
        super(client, credsProvider);
    }

    /**
     * 搜索人脸人体信息。 根据算法能力不同，某些请求字段在检索过程存在不生效的可能, 算法支持字段参见附录。 可指定或者不指定图片搜索人脸信息。 可指定或者不指定图片搜索人体信息。 增加检索时间段。
     * 支持size=0，当size=0是只返回总数，且是精确的。
     *
     * @param peopleFaceSearchRequest
     * @return
     */
    public PeopleFaceSearchResponse searchPeopleFace(PeopleFaceSearchRequest peopleFaceSearchRequest) {
        String xml = getXmlFromObject(peopleFaceSearchRequest, PeopleFaceSearchRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request = new VCMRequestMessageBuilder(getInnerClient()).setInputStream(in)
                .setInputSize(xml.getBytes().length).setEndpoint(endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.PEOPLE_FACES_SEARCH);
        PeopleFaceSearchResponse result = doOperation(request, peopleFaceSearchResponseParser, true);
        return result;
    }

    /**
     * 查询单动态库N中人脸出现的频次，按照频次降序排序。
     *
     * @param nvNRequest
     */
    public NvNResponse faceMatchNvN(NvNRequest nvNRequest) {
        String xml = getXmlFromObject(nvNRequest, NvNRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request = new VCMRequestMessageBuilder(getInnerClient()).setInputStream(in)
                .setInputSize(xml.getBytes().length).setEndpoint(endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.FACE_MATCH_NVN);
        return doOperation(request, nvNResponseParser, true);
    }

    /**
     * 根据taskId查询nvn结果集
     *
     * @param taskId
     * @return
     */
    public NvNResponse getFaceMatchNvN(String taskId) {
        Map<String, String> parameters = new HashMap<>(1);
        parameters.put("taskId", taskId);
        RequestMessage request = new VCMRequestMessageBuilder(getInnerClient()).setEndpoint(endpoint)
                .setParameters(parameters).setMethod(HttpMethod.GET).build();
        request.setResourcePath(VCMResourcePathConstants.FACE_MATCH_NVN);
        return doOperation(request, nvNResponseParser, true);
    }

    /**
     * 人脸1:1
     *
     * @param oneVOneRequest
     * @return
     */
    public OneVOneResponse faceOneVOne(OneVOneRequest oneVOneRequest) {
        String xml = getXmlFromObject(oneVOneRequest, OneVOneRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request = new VCMRequestMessageBuilder(getInnerClient()).setInputStream(in)
                .setInputSize(xml.getBytes().length).setEndpoint(endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.oneVOne);
        return doOperation(request, ResponseParsers.oneVOneResponseParser, true);
    }


    /**
     * 根据过脸记录Id查询过脸详细信息
     *
     * @param snapInfosRequest
     * @return
     */
    public SnapInfosResponse getFaceSnap(SnapInfosRequest snapInfosRequest) {
        String xml = getXmlFromObject(snapInfosRequest, SnapInfosRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request = new VCMRequestMessageBuilder(getInnerClient()).setInputStream(in)
                .setInputSize(xml.getBytes().length).setEndpoint(endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(VCMResourcePathConstants.snapInfos);
        return doOperation(request, snapInfosResponseParser, true);
    }

}
